<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" :model="userInfo" label-width="220px">
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <img :src="userInfo.timeOfEntry"> -->
            <!-- <UploadImg ref="staffPhoto" /> -->
            <UploadImg v-model="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSaveFn">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
// import { valid } from 'node_modules/mockjs/dist/mock'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        id: this.$route.query.id,
        staffPhoto: '',
        mobile: '',
        timeOfEntry: ''
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    async loadUserInfoById() {
      const { data } = await getUserDetailById(this.userInfo.id)
      console.log('获取个人信息', data)
      // eslint-disable-next-line no-prototype-builtins
      if (data.hasOwnProperty('staffPhoto') === false) {
        data.staffPhoto = ''
      }
      this.userInfo = data

      console.log(data)
      // 把头像地址,给子组件显示出来

      //   this.$refs.staffPhoto.imageUrl = res.data.staffPhoto
      console.log(data.staffPhoto)
    },
    async doSave() {
      try {
        // 从子组件中获取 新的头像地址 ,调用接口
        // const newUrl = this.$refs.staffPhoto.imageUrl
        // console.log('从子组件中获取 新的头像地址', newUrl)
        // 从子组件中拿到图片地址 存到自己的服务器
        // this.userInfo.staffPhoto = newUrl
        console.log(this.userInfo)
        const res = await saveUserDetailById(this.userInfo)
        console.log('保存的个人信息是', res)
        // 1 - 提示
        this.$message.success('保存成功')
        // 2 - 回到原来的页面
        this.$router.back()
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    hSaveFn() {
      this.$refs.userForm.validate(valid => {
        valid && this.doSave()
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>

